<template>
  <div class="app-container">
    <div class="head">
      <img src="@/assets/img/Layout/position.png" />
      <p class="see-block">品牌编辑</p>
    </div>
    <div class="view-container">
      <el-row class="status-title">
        <el-col class="mr20 width100">
          <el-button
            class="btn-success width100"
            type="primary"
            @click="submitForm"
          >保存</el-button>
        </el-col>
        <el-col class="mr20 width100">
          <el-button
            class="btn-success width100"
            type="primary"
            @click="cancel"
          >取消</el-button>
        </el-col>
      </el-row>
      <el-form ref="form" :model="form" label-width="140px" class="mt30">
          <el-row>
            <el-col :span="8">
              <el-form-item label="品牌编号:">
                <el-input v-model="form.brandCode" placeholder="" disabled />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="品牌中文名称:" prop="brandName">
                <el-input v-model="form.brandName" placeholder="请输入品牌中文名称" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="英文名称:" prop="englishName">
                <el-input v-model="form.englishName" placeholder="请输入英文名称" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 品牌LOGO -->
          <el-row>
            <el-col :span="8" style="display:flex;">
              <div class="upload-title">品牌LOGO:</div>
              <el-upload
                class="avatar-uploader"
                action="#"
                :auto-upload="false"
                :on-change="onUploadType"
                accept=".jpg,.jpeg,.png,.gif,.bmp,.JPG,.JPEG,.PNG,.GIF,.BMP"
                :show-file-list="false"
              >
                <img v-if="form.brandUrl" :src="form.brandUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="16">
              <el-form-item label="品牌简介:" prop="brandIntroduction">
                <el-input v-model="form.brandIntroduction" type="textarea" :autosize="{ minRows: 4}" placeholder="请输入品牌简介" maxlength="200" show-word-limit></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="16">
              <el-form-item label="备注:" prop="remark">
                <el-input v-model="form.remark" type="textarea" :autosize="{ minRows: 2}" placeholder="请输入内容" maxlength="200" show-word-limit></el-input>
              </el-form-item>
            </el-col>
          </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import { editBrand, detailBrand } from "@/api/system/basedata/brand/brand"
import { uploadImg } from "@/api/system/upload/upload"
export default {
  name: "brandEdit",
  data(){
    return {
      form: {
        brandName: '',
        englishName: '',
        brandUrl: '',
        brandIntroduction: '',
        remark: ''
      }
    }
  },
  created() {
    this.id = this.$route.query.id
    this.getDetailData()
  },
  methods: {
    // 获取详情信息
    getDetailData() {
      detailBrand(this.id).then(res => {
        this.form = res.data
      })
    },
    // 上传文件
    onUploadType(item){
      this.$modal.loading("正在上传")
      let formData = new FormData()
      let file = item.raw
      formData.append('file', file)
      uploadImg(formData).then(res => {
        this.$modal.closeLoading()
        this.$modal.msgSuccess("上传成功")
        this.form.brandUrl = res.data.fileUrl
      })
    },
    // 保存
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.$modal.loading("正在保存")
          editBrand(this.form).then(res=> {
            if(res.code == 200){
              this.$modal.msgSuccess("保存成功")
              this.$tab.closeOpenPage({ path: "/basedata/brand/detail", query:{id: this.id} })
              this.$modal.closeLoading()
            }
          })
        }
      })
    },
    // 取消
    cancel(){
      this.$tab.closeOpenPage({ path: "/basedata/brand/detail", query:{id: this.id} })
    }
  }
}
</script>

<style scoped>
.upload-title{
  width: 140px;
  padding-right: 10px;
  color: #606266;
  font-size: 14px;
  font-weight: bold;
  text-align: right;
}
.avatar-uploader{
  display: flex;
  align-items: center;
  justify-content: center;
  color:#606266;
  width: 150px;
  height: 150px;
  margin-bottom: 22px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader:hover {
  border-color: #5ED4D4;
}
.avatar-uploader:hover .avatar-uploader-icon{
  color: #5ED4D4;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}
.avatar {
  width: 150px;
  display: block;
}
</style>